// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var constant = require("constant");
    var css_detail = require("./myFinanceEarning.css");
    var databind = require("databind");
    var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
    
    var init = function(){
    	router.init();
    	inquireCommissionExtractApplicationPageList();
		initEvent();
    };
    
	var initEvent = function(){
		if(base.getData("sid")==null){
			location.href="../login/login.html";
		}
		
		$("#myFinanceEarning .extract").unbind("click").click(function(){
			if ($("#myFinanceEarning input[type=checkbox]:checked").length <= 0) {
				base.alert(2, "请选择需要提取佣金的融资!");
				return;
			}
			var financeIds = [];
			$.each($("#myFinanceEarning input[type=checkbox]:checked"), function() {
				var f = $(this).parents("tr").attr("financeId");
				financeIds.push(f);
			});
			createFinanceMoneyStatementEarning(financeIds);
		});
    };
    
    var inquireCommissionExtractApplicationPageList = function(){
		base.fnAjax({
			action: "inquireCommissionExtractApplicationPageList.json",
			async: false,
			oData: {
                pageSize: pageSize,
           		pageIndex: pageIndex
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#myExtractRecord script").html();
					var html = juicer(tpl, data);
					$("#myExtractRecord table").html(html);
					
					$("#myExtractRecord .detail").unbind("click").click(function(){
						router.route("account/commissionExtractDetail.html?extractId=" + $(this).parents("tr").attr("extractId"));
					});
                	
                	totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
                	base.pageControl($("#myExtractRecord .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireCommissionExtractApplicationPageList();
						});
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
	//企业查询融资信息列表
    var createFinanceMoneyStatementEarning = function(financeIds){
		base.fnAjax({
			action: "createFinanceMoneyStatementEarning.json",
			async: false,
			oData: {
           		financeIds: financeIds
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	base.alert(1, "生成成功！");
                	router.route("account/myFinanceEarning.html");
                } else {
//                  base.alert(2,"信息获取失败");
                }
            }
		});
	};
	
    return {
        init:init
    };
});